package com.snake.user.web.controller;

import com.snake.user.model.User;
import com.snake.user.service.UserService;
import com.snake.user.service.UserServiceImpl;
import com.snake.web.mvc.controller.PageController;
import org.apache.commons.validator.GenericValidator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.io.IOException;

/**
 * @author xsyang
 * @Title:
 * @date 2021-03-03 16:47
 */
@Path("/user")
public class UserController implements PageController {

    @Override
    @GET
    @Path("/register")
    public String execute(HttpServletRequest request, HttpServletResponse response){
        UserService userService = new UserServiceImpl();
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //密码校验
        if (!GenericValidator.minLength(password, 6) || !GenericValidator.maxLength(password, 32)) {
            return "fail.jsp";
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.save(user);
        return "success.jsp";
    }
}
